package 富婆梦.第一阶段.Java基础视频学习.线程的练习.作业4;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.线程的练习.作业四
 * @ClassName: LuckDraw
 * @Author: hemengjin
 * @Description: 有一个抽奖池, 该抽奖池中存放了奖励的金额, 该抽奖池用一个数组int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
 * 创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”，“抽奖箱2”，随机从arr数组中获取奖项元素并打印在控制台上,格式如下:
 * <p>
 * 抽奖箱1 又产生了一个 10 元大奖
 * 抽奖箱2 又产生了一个 100 元大奖
 * <p>
 * //.....
 * @Date:2021/11/3 19:33
 * @Version: 1.0
 */
public class LuckDraw implements Runnable {
    int[] arr = {10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300};
    int count = 1;
    List threadList = new ArrayList();

    private void draw() {
        Random r = new Random();
        System.out.println("抽奖箱" + Thread.currentThread().getName() + "：又产生一个" + arr[r.nextInt(arr.length)] + "元大奖");
        count++;
        if (count > 2) {
            count = 1;
        }
    }

    @Override
    public void run() {
        while (true) {
            if (Integer.parseInt(Thread.currentThread().getName()) == count) {
                draw();
            }
        }
    }


    public static void main(String[] args) {
        LuckDraw luckDraw = new LuckDraw();
        Thread t1 = new Thread(luckDraw);
        Thread t2 = new Thread(luckDraw);
        t1.setName("1");
        t2.setName("2");
        t1.start();
        t2.start();
        for (int i=0;i<10;i++){
            System.out.println("");
        }
    }
}
